package prictice.transaction1;

import org.apache.storm.transactional.ITransactionalSpout;
import org.apache.storm.utils.Utils;

import java.math.BigInteger;

/**
 * @Package prictice.transaction1
 * @Description:
 * @Author elwyn
 * @Date 2017/9/16 19:49
 * @Email elonyong@163.com
 */
public class MyCoordinator implements ITransactionalSpout.Coordinator<MyMate> {
    public static int BATCH_NUM =10;

    @Override
    public MyMate initializeTransaction(BigInteger bigInteger, MyMate myMate) {
        long beginPoint;
        if (myMate == null) {
            beginPoint = 0;
        } else {
            beginPoint = myMate.getBeginPoint() + myMate.getNum();
        }
        MyMate myMate1 = new MyMate();
        myMate1.setBeginPoint(beginPoint);
        myMate1.setNum(BATCH_NUM);
        System.out.println("启动一个事务" + myMate1.toString());
        return myMate1;
    }

    @Override
    public boolean isReady() {
        Utils.sleep(2000);
        return true;
    }

    @Override
    public void close() {

    }
}
